package rabbit.common;

import java.io.*;
import java.net.URI;
import java.net.URL;
import java.util.Properties;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by root on 17-4-16.
 */
public class rabbitMqPropertiesHelper {

    private static rabbitMqPropertiesHelper _instance;
    private static Lock lock = new ReentrantLock();

    public static rabbitMqPropertiesHelper getInstance(){
        try{
            lock.lock();

            if(_instance==null) {
                _instance = new rabbitMqPropertiesHelper();
            }
            return _instance;
        }
        finally {
            lock.unlock();
        }
    }



    private String propertiesFile="rabbitMqConfig.properties";
    private String defaultConfigPath = "/home/jiwen/Documents/intelijiWorkSpace/mq/src/main/resources/rabbitMqConfig.xml";
    private Properties prop;

    private rabbitMqPropertiesHelper(){
        URL folder = rabbitMqPropertiesHelper.class.getProtectionDomain().getCodeSource().getLocation();
        File propFile = new File(folder.getPath()+propertiesFile);
        if(propFile.exists()){
            try {
                InputStream is = rabbitMqPropertiesHelper.class.getClassLoader().getResourceAsStream(this.propertiesFile);
                prop = new Properties();
                prop.load(is);
            } catch (FileNotFoundException e) {
                System.out.println(e.getMessage());
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }else{
            System.out.println("Property file not found, using default parameters");
        }
    }

    public String getConfigPath(){
        if(this.prop!=null){
            return this.prop.getProperty("configFile");
        }else{
            return this.defaultConfigPath;
        }
    }
}
